package com.limox.bishe.viewgroup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.limox.bishe.R;
import com.limox.bishe.db.DataBaseManager;
import com.limox.bishe.util.Util;

import dalvik.system.BaseDexClassLoader;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class ClassroomLeft extends Fragment {

	private String teacher;
	private String courname;
	private String courroom;
	private SimpleAdapter adapter;

	OnListItemClickListenner mCallback;
	private List<Map<String, String>> listdata;

	// 定义一个接口
	public interface OnListItemClickListenner {
		public void onSelect(String ID, String title, String note);

		public void toggle();

	}

	public ClassroomLeft(String teacher, String courname, String courroom) {
		this.teacher = teacher;
		this.courname = courname;
		this.courroom = courroom;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setRetainInstance(true);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.act_roomleft, null);
		TextView textName = (TextView) view.findViewById(R.id.text_username);
		textName.setText(Util.ReadSharedPerference(getActivity(), "jwinfo",
				"user_name"));

		ListView list = (ListView) view.findViewById(R.id.list_note);
		listdata = new ArrayList<Map<String, String>>();

		getNoteFromDb();

		adapter = new SimpleAdapter(getActivity(), listdata,
				R.layout.note_list_item, new String[] { "title" },
				new int[] { R.id.text_title });

		list.setAdapter(adapter);
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				mCallback.onSelect(listdata.get(arg2).get("id"),
						listdata.get(arg2).get("title").toString(), listdata
								.get(arg2).get("note"));
			}
		});

		// 点名通知好友
		LinearLayout linear_call = (LinearLayout) view
				.findViewById(R.id.linear_call);
		linear_call.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Intent.ACTION_SEND);
				intent.setType("text/plain");// intent.setType("text/plain");
				intent.putExtra(Intent.EXTRA_SUBJECT, "好友推荐");
				intent.putExtra(Intent.EXTRA_TEXT, teacher + "老师的课：<"
						+ courname + ">点名啦！赶快来，说不定还有得救，上课地点是" + courroom + "！");
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(Intent.createChooser(intent, getActivity()
						.getTitle()));

			}
		});

		return view;
	}

	/**
	 * 从数据库中获取笔记
	 */
	private void getNoteFromDb() {
		// TODO Auto-generated method stub
		// 从数据库中读取课表信息（根据时间）
		DataBaseManager.CreateDatabase(getActivity(), "kb", null);
		SQLiteDatabase SQLdb = DataBaseManager.OpenDb(getActivity(), "kb",
				null, 3);
		Cursor cur = DataBaseManager.select_note(SQLdb, courname);

		if (cur.getCount() != 0) {
			for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("id", String.valueOf(cur.getInt(0)));
				map.put("title", cur.getString(1));
				map.put("note", cur.getString(2));
				map.put("courname", cur.getString(3));
				listdata.add(map);
			}
			// 打开抽屉
			mCallback.toggle();

		} else {
			Toast.makeText(getActivity(), "该课程还没有写笔记，写点心得吧", 0).show();
		}
		cur.close();
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		mCallback = (OnListItemClickListenner) activity;
	}

	/**
	 * 更新界面
	 */
	public void refesh() {
		// TODO Auto-generated method stub
		// 清空listview，重新加载数据 
		listdata.clear();
		getNoteFromDb();
		adapter.notifyDataSetChanged();

	}

}
